/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.commons.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5 加密工具类
 * 
 * @author wy
 * @version $Id: MD5Util.java, v 0.1 2012-6-11 下午2:31:44 wy Exp $
 */
public final class MD5Util {
    
    /**默认间隔符号*/
    public final static String DEFAULT_SPLIT = "$";

    /**
     * MD5 加密字符串
     * 
     * @param str
     * @return
     * @throws NoSuchAlgorithmException 
     * @throws UnsupportedEncodingException 
     */
    public static String encrypt(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(str.getBytes("UTF-8"));
        byte[] byteArray = messageDigest.digest();
        StringBuffer md5StrBuff = new StringBuffer();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }
    
    /**
     * 加密密码
     * 
     * @param prefix 前缀
     * @param username 用户名
     * @param password 密码
     * @param split 间隔符号
     * @return
     * @throws UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     */
    public static String encrypt(String prefix,String username,String password,String split) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        return encrypt(prefix+split+username+split+password);
    }

    /**
     * 加密密码
     * 
     * @param prefix 前缀
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     */
    public static String encrypt(String prefix,String username,String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        return encrypt(prefix+DEFAULT_SPLIT+username+DEFAULT_SPLIT+password);
    }
    
    /**
     * 验证密码
     * 
     * @param prefix 前缀
     * @param username 用户名
     * @param password 密码
     * @param split 间隔符号
     * @param encryptPwd 已加密的字符串
     * @return
     * @throws UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     */
    public static boolean validate(String prefix,String username,String password,String split,String encryptPwd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        String pwd = encrypt(prefix, username, password, split);
        return pwd.equals(encryptPwd);
    }
    
    /**
     * 验证密码
     * 
     * @param prefix 前缀
     * @param username 用户名
     * @param password 密码
     * @param encryptPwd 已加密的字符串
     * @return
     * @throws UnsupportedEncodingException 
     * @throws NoSuchAlgorithmException 
     */
    public static boolean validate(String prefix,String username,String password,String encryptPwd) throws NoSuchAlgorithmException, UnsupportedEncodingException{
        String pwd = encrypt(prefix, username, password);
        return pwd.equals(encryptPwd);
    }
    
}
